#
#  Product controller
#

from dzutil import message
from common.models.user import *
from views.user_view import *
from MySQLdb import IntegrityError

#------------------------------------------------------------------------
class ProductController:

    def _index(self):
        return ListView()

    def _show(self,key):
        user = Users().seek(key)
        return ShowView(user)

    def _edit(self,key):
        user = Users().seek(key)
        return EditView(user)

    def _confirm_delete(self,key):
		return DeleteView(Users().seek(key))

    def _change_password(self,key):
		return ChangePasswordView(Users().seek(key))

    def _update_password(self,key,USERID,NEW_PASSWORD,CONFIRM):
        user = Users().seek(USERID)
        if not NEW_PASSWORD or NEW_PASSWORD <> CONFIRM:
            message('Passwords do not match')
            return ChangePasswordView(user)
        else:
            user.set_password(NEW_PASSWORD)
            user.save()
            message('password changed')
        return ShowView(user)

    def _delete(self,USERID,**args):
        users = Users()
        users.delete(USERID)
        message('Deleted')
        return ListView()

    def _add(self):
        return AddView()

    def _update(self,USERID,**args):
        user = Users().seek(USERID)
        user.update(args)
        try:
            user.save()
#            Users().update(user)
        except IntegrityError:
            message('User loginid exists')
            return EditView(user)
        message('Updated')
        return ShowView(user)

    def _insert(self,**args):
        new_user = User()
        new_user.update(args)
        users = Users()
        try:
            newid = users.insert(new_user)
        except IntegrityError:
            message('User loginid exists')
            return AddView(new_user)
        message('New user saved')
        return ShowView(new_user)


result = ProductController()

